﻿//================================================================================
// Filename: Main.as
// Authour: Jacky Fong
// Description:
// Utilize plugins and read from xml to write an application
//================================================================================

package
{	
    import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.*;
	import PluginManager;

	public class Main extends Sprite
	{
		private var _pluginMgr:PluginManager;
		private var _actionQueue:Object;
		
		public function Main()
		{
			_init();
		}
		
		private function _init():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
						
			_pluginMgr = new PluginManager( this );
			_actionQueue = new Object();
			_setupEventListeners();
			_pluginMgr.loadXML( "data/plugins.xml" );
		}
		
		private function _setupEventListeners():void
		{
			_pluginMgr.addEventListener( "LoadPluginXMLComplete", _onLoadPluginXMLComplete );			
			_pluginMgr.addEventListener( "LoadPluginComplete", _onLoadPluginComplete );
			_pluginMgr.addEventListener( "LoadPluginsComplete", _onLoadPluginsComplete );
			
			_pluginMgr.addEventListener( "PagePlugin_ShowPageComplete", _onPagePlugin_ShowPageComplete );			
		}
		
		private function _onLoadPluginXMLComplete( evt:Event ):void
		{
			trace( this + " LoadPluginXMLComplete " + evt );
			// load core plugins
			_pluginMgr.loadPlugins( 1 );
		}
		
		private function _onLoadPluginComplete( evt:PluginEvent ):void
		{
			trace( this + " LoadPluginComplete " + evt );			
		}
		
		private function _onLoadPluginsComplete( evt:PluginEvent ):void
		{
			trace( this + " LoadPluginsComplete " + evt );
			
			switch( evt.args.priority ) {
				case 1: {
					// todo: call plugins' own load ui function
					_pluginMgr.addEventListener( "LoggerPlugin_LoadComplete", _onLoggerPluginLoadComplete );
					_pluginMgr.dispatchEvent( new PluginEvent( "LoggerPlugin_Load", { url:"ui/Logger.swf" } ) );
					break;
				}
				case 2: {
					_pluginMgr.addEventListener( "LoadingProgressPlugin_LoadComplete", _onLoadingProgressPluginLoadComplete );
					_pluginMgr.dispatchEvent( new PluginEvent( "LoadingProgressPlugin_Load", { url:"ui/LoadingProgress.swf" } ) );
					break;
				}
				case 3: {
					_doPluginActions();
					break;
				}
				default:
			}
		}
	
		private function _checkActionQueue( action:String ):uint
		{
			var a;
			for( a in _actionQueue ) {
				if( a != action ) continue;
				delete _actionQueue[ a ];
				break;
			}
			
			var count:uint = 0;
			for( a in _actionQueue ) {
				count++;
			}
			return count;
		}
		
		// loading done, start play
		private function _play():void
		{
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_ShowBackground", { show:true } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_ShowPage", { page:"home" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "NavigationBarPlugin_Show", { show:true } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "LoadingProgressPlugin_Show", { show:false } ) );			
		}
		
		private function _doPluginActions():void
		{	
			_pluginMgr.addEventListener( "PagePlugin_SetBackgroundComplete", _onPagePlugin_SetBackgroundComplete );
			_pluginMgr.addEventListener( "PagePlugin_AddPageElementComplete", _onPagePlugin_AddPageElementComplete );
			_pluginMgr.addEventListener( "PhotoAlbumPlugin_LoadAlbumsComplete", _onPhotoAlbumPlugin_LoadAlbumsComplete );
			
			_actionQueue[ "PagePlugin_SetBackgroundComplete" ] = false;
			_actionQueue[ "PhotoAlbumPlugin_LoadAlbumsComplete" ] = false;
						
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_SetBackground", { url:"images/bg.jpg" } ) );
			
			// home
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPage", { page:"home" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"home", url:"images/first_taste.png", x:219, y:272 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"home", url:"images/bible_genesis_2_18.png", x:15, y:110 } ) );
			
			// story
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPage", { page:"story" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"story", url:"images/our_story.png", x:768, y:120 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"story", url:"images/bible_john1_4_19.png", x:15, y:110 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageShape",
														{ page:"story", shape:"rect", colour:0xf7f2c0, alpha:0.5,
														borderSize:0, borderColour:0, borderAlpha:0,
														x:0, y:160, width:1024, height:510 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"story", url:"ui/OurStory.swf", x:0, y:160 } ) );
			
			// invitation
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPage", { page:"invitation" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"invitation", url:"images/our_invitation.png", x:680, y:120 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"invitation", url:"images/bible_john1_4_19.png", x:15, y:110 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageShape",
														{ page:"invitation", shape:"rect", colour:0xf7f2c0, alpha:0.5,
														borderSize:0, borderColour:0, borderAlpha:0,
														x:0, y:160, width:1024, height:510 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"invitation", url:"ui/OurInvitation.swf", x:0, y:160 } ) );
			
			// photo
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPage", { page:"photo" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"photo", url:"images/our_photo.png", x:768, y:120 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"photo", url:"images/bible_john1_4_19.png", x:15, y:110 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageShape",
														{ page:"photo", shape:"rect", colour:0xf7f2c0, alpha:0.5,
														borderSize:0, borderColour:0, borderAlpha:0,
														x:0, y:160, width:1024, height:510 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PhotoAlbumPlugin_SetPage", { page:"photo" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PhotoAlbumPlugin_LoadAlbums", { url:"data/albums.xml" } ) );
			
			// guestbook
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPage", { page:"guestbook" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"guestbook", url:"images/guestbook.png", x:755, y:120 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElement", { page:"guestbook", url:"images/bible_john1_4_19.png", x:15, y:110 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_AddPageShape",
														{ page:"guestbook", shape:"rect", colour:0xf7f2c0, alpha:0.5,
														borderSize:0, borderColour:0, borderAlpha:0,
														x:0, y:160, width:1024, height:510 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "MessageBoardPlugin_SetPage", { page:"guestbook" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "MessageBoardPlugin_Connect", { url:"http://www.henviv.com/flashservices/gateway.php" } ) );
														
			//_pluginMgr.dispatchEvent( new PluginEvent( "PagePlugin_ShowPage", { page:"home" } ) );
			
			_pluginMgr.addEventListener( "NavigationBarPlugin_LoadComplete", _onNavigationBarPluginLoadComplete );
			_actionQueue[ "NavigationBarPlugin_LoadComplete" ] = false;
			_pluginMgr.dispatchEvent( new PluginEvent( "NavigationBarPlugin_Load", { url:"ui/NavigationBar.swf" } ) );
			
			// show at last
			//_pluginMgr.dispatchEvent( new PluginEvent( "LoggerPlugin_Show", { show:true } ) );
		}
		
		private function _onLoggerPluginLoadComplete( evt:PluginEvent ):void
		{
			_pluginMgr.dispatchEvent( new PluginEvent( "LoggerPlugin_SetPosition", { x:600, y:0 } ) );
			// load high priority plugins
			_pluginMgr.loadPlugins( 2 );
		}
		
		private function _onPagePlugin_SetBackgroundComplete( evt:PluginEvent ):void
		{
			if( _checkActionQueue( evt.type ) == 0 ) { _play(); }
		}

		private function _onPagePlugin_AddPageElementComplete( evt:PluginEvent ):void
		{
			//_pluginMgr.apiLog( "!!!!!!! " + evt );
		}
		
		private function _onPhotoAlbumPlugin_LoadAlbumsComplete( evt:PluginEvent ):void
		{
			//_pluginMgr.apiLog( this + " LoadAlbumsComplete " + evt );
			if( _checkActionQueue( evt.type ) == 0 ) { _play(); }
		}
		
		private function _onLoadingProgressPluginLoadComplete( evt:PluginEvent ):void
		{		
			_pluginMgr.dispatchEvent( new PluginEvent( "LoadingProgressPlugin_SetPosition", { x:stage.stageWidth-200, y:stage.stageHeight-220 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "LoadingProgressPlugin_Show", { show:true } ) );
			// load normal priority plugins
			_pluginMgr.loadPlugins( 3 );
		}
		
		private function _onNavigationBarPluginLoadComplete( evt:PluginEvent ):void
		{
			_pluginMgr.dispatchEvent( new PluginEvent( "NavigationBarPlugin_SetPosition", { x:0, y:680 } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "NavigationBarPlugin_SetLink", { button:"mcHome", page:"home" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "NavigationBarPlugin_SetLink", { button:"mcStory", page:"story" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "NavigationBarPlugin_SetLink", { button:"mcInvitation", page:"invitation" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "NavigationBarPlugin_SetLink", { button:"mcPhoto", page:"photo" } ) );
			_pluginMgr.dispatchEvent( new PluginEvent( "NavigationBarPlugin_SetLink", { button:"mcGuestbook", page:"guestbook" } ) );
			
			if( _checkActionQueue( evt.type ) == 0 ) { _play(); }
		}
		
		private function _onPagePlugin_ShowPageComplete( evt:PluginEvent ):void
		{
			//_pluginMgr.apiLog( this + " ShowPageComplete " + evt );
			switch( evt.args.page ) {
				case "guestbook": {
					_pluginMgr.dispatchEvent( new PluginEvent( "MessageBoardPlugin_BrowseHeader", { startId:-1, nMsg:16 } ) );
					_pluginMgr.dispatchEvent( new PluginEvent( "MessageBoardPlugin_ReadMessage", { id:-1 } ) );
					break;
				}
			}
		}
	}
}